package pers.vankid.authchelper.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import pers.vankid.authchelper.helper.AuthcHelper;
import pers.vankid.authchelper.helper.AuthcInitHelper;
import pers.vankid.common.model.authc.AuthcUser;
import pers.vankid.common.model.authc.AuthcUserResponse;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.http.HttpUtil;

public class AuthcHelperImpl implements AuthcHelper {

    private AuthcInitHelper authcInitHelper;
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    public void setAuthcInitHelper(AuthcInitHelper authcInitHelper) {
        this.authcInitHelper = authcInitHelper;
    }

    /**
     * 登陆
     *
     * @param authcUser
     * @return
     */
    @Override
    public String login(AuthcUser authcUser) throws Exception {
        AuthcUser authcUserSaved = authcInitHelper.getAuthcUser(authcUser.getAuthcKey());
        if (authcUserSaved == null || !authcUserSaved.getPwd().equals(authcUser.getPwd())) {
            CommonResponse<String> commonResponse = gson.fromJson(
                    HttpUtil.sendPost(authcInitHelper.getSessionKeeperUrl() + "/login/", gson.toJson(authcUser))
                    , new TypeToken<CommonResponse<String>>() {
                    }.getType());
            if (commonResponse.doSuccess())
                return commonResponse.getData();
            else throw new Exception(commonResponse.getMsg());
        } else throw new Exception("用户名密码不匹配！");
    }

    /**
     * 注销
     *
     * @param sign
     * @return
     */
    @Override
    public void layout(String sign) throws Exception {
        if (sign == null || sign.isEmpty())
            throw new Exception("sign不能为空！");
        CommonResponse<String> commonResponse = gson.fromJson(
                HttpUtil.sendPost(authcInitHelper.getSessionKeeperUrl() + "/layout/" + sign, "")
                , new TypeToken<CommonResponse<String>>() {
                }.getType());
        if (!commonResponse.doSuccess())
            throw new Exception(commonResponse.getMsg());

    }

    /**
     * 检查登陆状态
     *
     * @param sign
     * @return
     */
    @Override
    public boolean checkLogin(String sign) throws Exception {
        if (sign == null || sign.isEmpty())
            return false;
        CommonResponse<Boolean> commonResponse = gson.fromJson(
                HttpUtil.sendPost(authcInitHelper.getSessionKeeperUrl() + "/check/" + sign, "")
                , new TypeToken<CommonResponse<Boolean>>() {
                }.getType());
        if (commonResponse.doSuccess())
            return commonResponse.getData();
        else
            throw new Exception(commonResponse.getMsg());
    }

    /**
     * 获取登陆用户数据
     *
     * @param sign
     * @return
     * @throws Exception
     */
    @Override
    public AuthcUserResponse getAuthcData(String sign) throws Exception {
        CommonResponse<AuthcUserResponse> commonResponse = gson.fromJson(
                HttpUtil.sendGet(authcInitHelper.getSessionKeeperUrl() + "/" + sign)
                , new TypeToken<CommonResponse<AuthcUserResponse>>() {
                }.getType());
        if (commonResponse.doSuccess())
            return commonResponse.getData();
        else throw new Exception(commonResponse.getMsg());
    }

    /**
     * 修改登陆用户数据
     *
     * @param sign
     * @param authcUserResponse
     * @return
     * @throws Exception
     */
    @Override
    public void updateAuthcData(String sign, AuthcUserResponse authcUserResponse) throws Exception {
        CommonResponse<String> commonResponse = gson.fromJson(
                HttpUtil.sendPost(authcInitHelper.getSessionKeeperUrl() + "/user/" + sign, gson.toJson(authcUserResponse))
                , new TypeToken<CommonResponse<String>>() {
                }.getType());
        if (!commonResponse.doSuccess())
            throw new Exception(commonResponse.getMsg());
    }
}
